#include "unp.h"

void error(char *msg) {
	printf("%s\n", msg);
	exit(1);
}

int main(void) {

	int sockfd;
	struct sockaddr_in servaddr;

	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
		error("socket failed");

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(13000);

	if ( bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 )
		error("bind error");

	if ( listen(sockfd, 10) < 0 ) 
		error("listen error");

	while ( 1 ) {
		char buf[] = "HELLO WORLD!";
		int connfd = accept(sockfd, (struct sockaddr *)NULL, NULL);
		if ( connfd < 0 ) 
			error("connection error");

		write(connfd, buf, strlen(buf));
		close(connfd);
	}

	close(sockfd);
	return 0;
}
